<template>
	<view style="background-color: #F2F2F5;" v-if="showadd()">
		<view class="main">
			<u-modal :show="show" title="确定删除这条记录吗" :showCancelButton="true" @confirm="confirm"
				@cancel="cancel"></u-modal>

			<view class="pubcontent">
				<textarea auto-height v-model="content" placeholder-style="color:#F76260" placeholder="写点什么.." />
			</view>
			<view class="editpicture">
				<!-- <video id="2" style="width:100%;height:350rpx;" @play="playFullscreen()" @fullscreenchange="fullchange"
					object-fit=cover :src="videourl"></video> -->
			</view>
			<view class="edittime">
				<view class="textarea" style="flex: 1;padding: 15rpx; ">
					<uni-icons type="redo" size="22" style=" vertical-align: middle;">
					</uni-icons>
					<text>记录时间</text>
				</view>
				<view class="timearea" style="flex: 1;text-align: end; padding: 15rpx;" @click="opendatepop()">
					<text style="white-space: nowrap; overflow: hidden;">{{pubtime}}</text>
					<uni-icons type="arrow-right" size="22" style="vertical-align:text-bottom;color: red;">
					</uni-icons>
				</view>

			</view>

			<view class="savebutton">
				<button @click="pub" class="mini-btn" size="default" type="warn">保存</button>
			</view>

			<view class="delbutton" v-if="optype=='edit'" @click="del">
				<text>删除这条记录</text>
			</view>

			<choicedate :selectedDate="dischargeTime" @dateSelected="setDischargeTime" @popType="datesPopFunc"
				v-if="datesPop" />
		</view>


	</view>

</template>

<script>
	import * as utils from '../../util/util.js';

	export default {
		data() {
			return {
				show: false,
				id: "",
				percentage: "0", //进度条
				complete: 0, //
				optype: "add", //添加还是编辑
				content: '',
				dischargeTime: '', //选择的空间 yyyy-mm-dd
				pubtime: '',
				datesPop: false, //时间弹层控制
				videourl: "",
				editpicurl: []
			}
		},
		onLoad(option) {
			//编辑
			if (option.type == 'edit') {
				console.log("编辑" + option.id)
				this.id = option.id;
				let _this = this;
				this.optype = 'edit'
				utils.getListById(option.id).then(res => {
					const data = res.result.data[0];
					_this.content = data.content;
					_this.pubtime = data.recordtime.replace(/(\d{4})\-(\d{2})\-(\d{2})/, "$1年$2月$3日");
					_this.dischargeTime = data.recordtime;
					_this.videourl = data.url[0];
				})
				return;
			}
			const queryStringData = JSON.parse(decodeURIComponent(option.path))
             console.log(queryStringData);
			 this.videourl = queryStringData.fileID;
			this.pubtime = utils.formatDate(new Date());
			this.dischargeTime = utils.formatDateyyymmdd(new Date());
		},
		methods: {
			playFullscreen() {
				uni.createVideoContext("2", this).requestFullScreen();
			},

			fullchange(e) {
				console.log(e.currentTarget.id);
				if (!e.detail.fullScreen) {
						const v = uni.createVideoContext(e.currentTarget.id, this);
						v.pause();
						v.exitFullScreen();
				}

			},

			del() {
				this.show = true;
			},
			showadd(){
				return utils.show();
			},

			confirm() {
				var _this = this;
				uniCloud.callFunction({
					name:"delpublish",
					data:{
						id:_this.id
					}
				}).then(res=>{
					uni.reLaunch({
						url:'/pages/index/index'
					})
				}).catch(er=>{
					utils.toastwarn("删除失败"+er)
				})
			},
			cancel() {
				this.show = false;
			},

			opendatepop() {
				this.datesPop = true;
			},
			setDischargeTime(date) {
				console.log(date);
				let arr = date.split('-');
				this.pubtime = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
				this.datesPop = false;
				this.dischargeTime = date;
			},
			datesPopFunc() {
				this.datesPop = false;
			},

			pub() {
				var that = this;
				if (this.content.trim().length == 0) {
					utils.toastwarn("请填写内容", 1000);
					return;
				}
				uni.showLoading({
					"title": "处理中",
					"mask": true
				});
				if (this.optype == 'add') {
					let para = {
						"username": utils.getLoginInfo().username,
						"userid": utils.getLoginInfo().id,
						"content": that.content,
						"recordtime": that.dischargeTime,
						"createtime": Date.now(),
						"url": [this.videourl],
						type: 'video'
					};
					console.log(para);
					utils.addpublish(para).then(res => {
						uni.hideLoading();
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}).catch(error => {
						utils.toastwarn("编辑出错", 1000);
					})

				} else {
					let para = {
						"id": that.id,
						"content": that.content,
						"recordtime": that.dischargeTime,
						"url": [this.videourl]
					};

					utils.editpublish(para).then(res => {
						uni.hideLoading();
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}).catch(er => {
						utils.toastwarn("更新出错", 1000);
					})
				}
				//编辑
			}


		}

	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		flex-direction: column;
		background-color: #F2F2F5;
		padding: 25rpx;

		.u-upload__wrap {
			padding: 10rpx;
		}

		.u-border {
			display: inline-block;
			border-style: none !important;
			border: none !important;
			padding: 0;
			margin: 0;
			background: transparent !important;
		}

		.u-border::after {
			border: none !important;
		}
	}

	.pubcontent {
		width: 700rpx;
		height: 200rpx;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		background-color: white;
		margin-top: 25rpx;
	}

	.editpicture {
		background-color: white;
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		width: 700rpx;
	}

	.edittime {
		width: 700rpx;
		display: flex;
		background-color: white;
		border-top: 1rpx solid #F2F2F5;
	}

	.savebutton {
		margin-top: 50rpx;

		button {
			display: block;
			//margin: 0 auto;
			background-color: #FF4D88;
			border-radius: 50rpx;
			height: 100rpx;
			font-weight: bold;
		}
	}

	.delbutton {
		margin-top: 50rpx;
		display: flex;
		justify-content: center;
		font-weight: bold;

		text {
			color: #FF4D88;
		}
	}
</style>